package com.mouse.view;

import com.mouse.commen.Message;
import com.mouse.commen.MessageType;
import com.mouse.po.User;

import javax.swing.*;
import java.awt.*;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerClient extends JFrame {

    public static void main(String[] args) {
        ServerClient serverClient = new ServerClient();
        serverClient.createJFrame();
    }

public void createJFrame() {
    JLabel jLabel = new JLabel("服务端已启动，监听8881端口，等待客户连接", JLabel.CENTER);
    this.add(jLabel, BorderLayout.CENTER);

    this.setTitle("QQ服务器");
    this.setBounds(505, 305, 350, 250);
    this.setVisible(true);
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


    public void server() throws Exception{

        ServerSocket serverSocket = new ServerSocket(8881);
        Socket socket = serverSocket.accept();

        InputStream inputStream = socket.getInputStream();
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
        User user = (User)objectInputStream.readObject();


        Message message = new Message();
        if(user.getUsername().equals("my")&&user.getPwd().equals("123456")){
            message.setMessageType(MessageType.LOGIN_SUCCESS);
        }else{
            message.setMessageType(MessageType.LOGIN_FAIT);
        }


        OutputStream outputStream = socket.getOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(message);
    }
}
